package com.practice.service.admin.impl;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.practice.common.context.BaseContext;
import com.practice.common.result.PageResult;
import com.practice.common.result.Result;
import com.practice.mapper.user.ArticleMapper;
import com.practice.mapper.user.CateMapper;
import com.practice.pojo.admin.dto.PageArticleDTO;
import com.practice.pojo.admin.vo.ArticleVO;
import com.practice.pojo.user.entity.Article;
import com.practice.pojo.user.entity.Cate;
import com.practice.service.admin.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;


@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private CateMapper cateMapper;

    @Override
    public Result deleteById(Long id) {
        articleMapper.deleteById(id);
        return Result.success();
    }

    /**
     * 文章列表-分页
     * @return
     */
    @Override
    public Result list(PageArticleDTO dto) {
        Long page = Long.valueOf(dto.getPage());
        Long limit = Long.valueOf(dto.getLimit());
        //开始分页
        IPage<Article> articlePage = new Page<>(page,limit);
        //模糊查询
        LambdaQueryWrapper<Article> lqw = new LambdaQueryWrapper<>();
        lqw.like(Article::getTitle,dto.getTitle()).eq(dto.getCateid()!="",Article::getCateid,dto.getCateid())
                .between(dto.getStart_time()!=null,Article::getCreateDate,dto.getStart_time(),dto.getEnd_time());
        articleMapper.selectPage(articlePage,lqw);
        List<ArticleVO> articleVOS = BeanUtil.copyToList(articlePage.getRecords(), ArticleVO.class);
        for (ArticleVO articleVO : articleVOS) {
            Cate cate = cateMapper.selectById(articleVO.getCateid());
            articleVO.setCatename(cate.getCatename());
        }

        PageResult pageResult = PageResult.builder()
                .count(articlePage.getTotal())
                .totalPages(articlePage.getPages())
                .pageSize(articlePage.getSize())
                .currentPage(articlePage.getCurrent())
                .data(articleVOS)
                .build();
        return Result.success(pageResult);
    }

    /**
     * 增添文章
     * @param article
     * @return
     */
    @Override
    public Result add(Article article) {
        //LocalDateTime转long
        long dateTime = System.currentTimeMillis();
        //创建时间
        article.setCreateDate(dateTime);
        //userId
        article.setUserId(BaseContext.getCurrentId());
        //默认数据
        article.setDefaultData("1");
        articleMapper.insert(article);
        return Result.success();
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @Override
    public Result selectById(Integer id) {
        Article article = articleMapper.selectById(id);
        return Result.success(article);
    }

    /**
     * 修改文章
     * @param article
     * @return
     */

    @Override
    public Result update(Article article) {
        articleMapper.updateById(article);
        return Result.success();
    }
}
